﻿using HP.UtilsLib.Privates;
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;
using Windows.Media.Devices;

namespace HP.UtilsLib.Devices
{
    public class VolumeControl:IVolumeControl
    {
        public void SetMute(bool isMute)
        {
            try
            {
                var masterVol = VolumneControlImpl.GetAudioEndpointVolumeInterface();
                masterVol.SetMute(isMute, Guid.Empty);
            }
            catch{}
        }
        public void SetVolume(double level)
        {
            if (level > 1) level = 1;
            if (level < 0) level = 0;

            try
            {
                var masterVol = VolumneControlImpl.GetAudioEndpointVolumeInterface();
                // Make sure that the audio is not muted
                if(level == 0)
                {
                    masterVol.SetMute(true, Guid.Empty);
                }
                else
                {
                    masterVol.SetMute(false, Guid.Empty);
                }
                float newAudioValue = Convert.ToSingle(level);
                masterVol.SetMasterVolumeLevelScalar(newAudioValue, Guid.Empty);
            }
            catch { }
        }

        public double GetVolume()
        {
            double level = 0;
            try
            {
                var masterVol = VolumneControlImpl.GetAudioEndpointVolumeInterface();
                level = Convert.ToDouble(masterVol.GetMasterVolumeLevelScalar());
            }
            catch { }

            return level;
        }
    }
}
